//
//  NSData+Friend.m
//  
//
//  Created by lwx376629 on 16/11/1.
//  Copyright © 2016年 Capfer. All rights reserved.
//

#import "NSData+Friend.h"
#import<CommonCrypto/CommonDigest.h>
@implementation NSData (Friend)
/**
 *  NSData类型转NSInteger类型
 */
- (NSInteger)integerValue {
    if (self.length == 1) {
        const char *pos =  [self bytes];
        int a;
        memcpy(&a, pos, sizeof(int));
        return a;
    } else if (self.length == 2) {
        uint16_t i;
        [self getBytes:&i length:sizeof(uint16_t)];
        if (NSHostByteOrder() == NS_LittleEndian){
            i = NSSwapHostShortToBig(i);
        }
        return i;
    } else if (self.length == 4) {
        uint32_t i;
        [self getBytes: &i length: sizeof(uint32_t)];
        if (NSHostByteOrder() == NS_LittleEndian) {
            i = NSSwapHostIntToBig(i);
        }
        return i;
    } else {
        return 0;
    }
}
/**
 *  NSData类型二进制的数字字符串
 */
- (NSString *)dataToHex2String {
    if (self.length > 1) return nil;
    NSString *str = [NSString stringWithFormat:@"%@",self];
    NSString *preStr = [self changeTo2Str:[str substringWithRange:NSMakeRange(1, 1)]];
    NSString *sufStr = [self changeTo2Str:[str substringWithRange:NSMakeRange(2, 1)]];
    return [preStr stringByAppendingString:sufStr];
}
- (NSString *)changeTo2Str:(NSString *)str {
    NSArray *arr1 = @[@"0000",@"0001",@"0010",@"0011",@"0100",@"0101",@"0110",@"0111",@"1000",@"1001",@"1010",@"1011",@"1100",@"1101",@"1110",@"1111"];
    NSArray *arr2 = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    NSArray *arr3 = @[@"a",@"b",@"c",@"d",@"e",@"f"];
    NSArray *arr4 = @[@"A",@"B",@"C",@"D",@"E",@"F"];
    __block NSString *string = @"";
    __block BOOL is = NO;
    [arr2 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqualToString:str]) {
            string = arr1[idx];
            is = YES;
            *stop = YES;
        }
    }];
    if (is == NO) {
        [arr3 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isEqualToString:str]) {
                string = arr1[idx + 10];
                is = YES;
                *stop = YES;
            }
        }];
    }
    if (is == NO) {
        [arr4 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isEqualToString:str]) {
                string = arr1[idx + 10];
                is = YES;
                *stop = YES;
            }
        }];
    }
    return string;
}

+ (uint16_t)NSDataToInt16ByBigEndian:(NSData *)data{
    uint16_t i;
    [data getBytes: &i length: sizeof(uint16_t)];
    
    if (NSHostByteOrder() == NS_LittleEndian)
    {
        i = NSSwapHostShortToBig(i);
    }
    return i;
}

+(int16_t)NSDataToNegativeInt16BigEndian:(NSData *)data{
    int16_t i;
    [data getBytes:&i length:sizeof(int16_t)];
    
    if (NSHostByteOrder() == NS_LittleEndian)
    {
        i = NSSwapHostShortToBig(i);
    }
    return i;
}

- (int64_t)integerValueWithSign {
    if (self.length == 1) {
        int16_t i;
        [self getBytes:&i length:sizeof(int8_t)];
        return i;
    } else if (self.length == 2) {
        int16_t i;
        [self getBytes:&i length:sizeof(int16_t)];
        if (NSHostByteOrder() == NS_LittleEndian){
            i = NSSwapHostShortToBig(i);
        }
        return i;
    } else if (self.length == 4) {
        int32_t i;
        [self getBytes: &i length:sizeof(int32_t)];
        if (NSHostByteOrder() == NS_LittleEndian) {
            i = NSSwapHostIntToBig(i);
        }
        return i;
    } else {
        return 0;
    }
}

+ (uint16_t)NSDataToInt16ByLittleEndian:(NSData *)data{
    uint16_t i;
    [data getBytes: &i length: sizeof(uint16_t)];
    
    if (NSHostByteOrder() == NS_BigEndian)
    {
        i = NSSwapHostShortToLittle(i);
    }
    return i;
}

/**
 * brief 将Data大端解析为整数
 * param data 待解析的数据
 * param isSigned 是否为有符号数据 YES:有符号 NO:无符号
 * returns int64_t类型的整数
 */
+ (int64_t)NSDataToIntByBigEndian:(NSData *)data isSigned:(BOOL)isSigned {
    NSInteger length = data.length;
    //如果不是8位、16位、32位数据则直接返回0
    if (length != 1 && length != 2 && length != 4) {
        return 0;
    }
    int64_t i = 0;
    [data getBytes:&i length:length];
    if (NSHostByteOrder() == NS_LittleEndian)
    {
        switch (length) {
            case 2:
                i = NSSwapHostShortToBig((int16_t)i);
                break;
            case 4:
                i = NSSwapHostIntToBig((int32_t)i);
                break;
            default:
                break;
        }
    }
    return i;
}

/**
 * brief 将Data小端解析为整数
 * param data 待解析的数据
 * param isSigned 是否为有符号数据 YES:有符号 NO:无符号
 * returns int64_t类型的整数
 */
+ (int64_t)NSDataToIntByLittleEndian:(NSData *)data isSigned:(BOOL)isSigned {
    NSInteger length = data.length;
    //如果不是8位、16位、32位数据则直接返回0
    if (length != 1 && length != 2 && length != 4) {
        return 0;
    }
    int64_t i = 0;
    [data getBytes:&i length:length];
    if (NSHostByteOrder() == NS_BigEndian)
    {
        switch (length) {
            case 2:
                i = NSSwapHostShortToLittle((int16_t)i);
                break;
            case 4:
                i = NSSwapHostIntToLittle((int32_t)i);
                break;
            default:
                break;
        }
    }
    return i;
}
/**
 * 获得MD5字符串
 */
- (NSString*)MD5 {
    const char *original_str = (const char *)[self bytes];
    unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16
    CC_MD5(original_str, (uint)strlen(original_str), digist);
    NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10];
    for(int  i =0; i<CC_MD5_DIGEST_LENGTH;i++){
        [outPutStr appendFormat:@"%02x",digist[i]];//小写x表示输出的是小写MD5，大写X表示输出的是大写MD5
    }
    return [outPutStr lowercaseString];
}
/**
 * 模型的反序列化
 */
- (TYZModel *)model {
    return [NSKeyedUnarchiver unarchiveObjectWithData:self];
}
@end
